﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using System.ServiceModel;

namespace Client
{
    public class ClientCalculatorService : ClientBaseService<ICalculator>
    {
        #region Property

        private static ClientCalculatorService _instance = null;
        private static readonly object SynObject = new object();
        private const string CalculatorServiceAddress = "calculatorservice";

        #endregion Property

        #region Construnction

        private ClientCalculatorService()
            : base()
        {
        }

        public static ClientCalculatorService Instance
        {
            get
            {
                if (null == _instance)
                {
                    lock (SynObject)
                    {
                        if (null == _instance)
                        {
                            _instance = new ClientCalculatorService();
                        }
                    }
                }
                return _instance;
            }
        }

        #endregion Construnction

        #region Override

        /// <summary>
        /// 采用通道工厂的方式生成客户端服务对象实例
        /// </summary>
        /// <param name="bind"></param>
        /// <param name="address"></param>
        private string CreateClientInstanceByChannel(params object[] parameter)
        {
            ICalculator _CalculatorService = GetChannelFactory(CalculatorServiceAddress);
            return base.CreateClientInstanceByChannel(_CalculatorService, parameter);
        }

        public override ICalculator GetChannelFactory(string Address)
        {
            return base.GetChannelFactory(Address);
        }

        #endregion Override

        #region method

        [MethodImpl(MethodImplOptions.NoInlining)]
        public string Add(string js)
        {
            return CreateClientInstanceByChannel(js);
        }

        [MethodImpl(MethodImplOptions.NoInlining)]
        public void UnRegist()
        {
            CreateClientInstanceByChannel();
        }

        #endregion method
    }
}